//
//  JiJinViewController.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/30.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "JiJinViewController.h"
#import "LiveListTableViewCell.h"
#import "WEBViewController.h"
#import "HTTPManagers.h"
#import "JiJinModel.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "JiJinTypeVC.h"

@interface JiJinViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableview;
@property (nonatomic,strong)NSMutableArray *videoArr;
@property (nonatomic,copy)NSString *timeString;
@property (nonatomic,strong)NSMutableArray *moreVideoArr;
@end

@implementation JiJinViewController
{
    NSInteger page;
    NSInteger type;
    NSInteger start;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    start = 0;
    if (self.isMoreVideo) {
        self.title = @"更多视频";
        [self getVideoData];
    }else{
        self.title = @"集锦";
        [self getData];
    
    }
}
#pragma mark - UI
-(void)setupUI{
    [self.view addSubview:self.tableview];
    if (self.isMoreVideo) {
        
    }else{
        [self setRightBtn];
    }
    
}
-(void)setRightBtn{
    UIImage *image = [UIImage imageNamed:@"fenlei"];
    
    UIButton *type = [UIButton buttonWithType:UIButtonTypeCustom];
    
    type.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    
    [type setBackgroundImage:image
                        forState:UIControlStateNormal];
    
    [type addTarget:self
                 action:@selector(typeClick)
       forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:type];
}
-(void)getData{
    
//    http://cdn.a8.tvesou.com/right/hot_video_list?
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//全部：0，NBA：1，英超：4，CBA：2，西甲：6，中超：9，德甲：3，意甲：5， 欧冠：8，法甲：7
    
    [HTTPManagers getWithLiveURL:@"hot_video_list" params:params success:^(id json) {
        [_tableview.mj_header endRefreshing];
        NSArray *arr= [json objectForKey:@"data"];
        if (self.videoArr.count>0) {
            [_videoArr removeAllObjects];
        }
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            JiJinModel *model = [JiJinModel mj_objectWithKeyValues:dict];
            [_videoArr addObject:model];
            
        }
        [_tableview reloadData];
        
    } failure:^(NSError *error) {
        
    }];

    
}
-(void)getVideoData{
    //    http://101.200.31.130/free_kicker/apis/video/getVideosByPage?api=v2&deviceId=ios&login_token=&lat=0.000000&lon=0.000000&login_user_id=0&uuid=41723BB4-EB3F-4C39-85B3-57578C09E997&referer=iosreferer&version=ios_3.2.1&start=0&count=10
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"api"]=@"v2";
    params[@"deviceId"] = @"ios";
    params[@"login_token"]= @"";
    params[@"lat"] = @"0.000000";
    params[@"lon"] = @"0.000000";
    params[@"login_user_id"] = @"0";
    params[@"uuid"]=@"41723BB4-EB3F-4C39-85B3-57578C09E997";
    params[@"referer"] = @"iosreferer";
    params[@"version"] = @"ios_3.2.1";
    params[@"start"] =@(start);
    params[@"count"] =@"10";
    
    [HTTPManagers getWithHomeVideo:@"getVideosByPage" params:params success:^(id json) {
        [_tableview.mj_header endRefreshing];
        NSArray *arr = json[@"datas"];
        for (int i = 0 ; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            [self.moreVideoArr addObject:dict];
        }
        [_tableview reloadData];
        
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
-(void)getTypeData{
    
    //    http://cdn.a8.tvesou.com/right/hot_video_list?
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
//    0 全部 1nba 2cba 3德甲 4英超 5意甲 6西甲 7法甲 8欧冠 9中超
//    http://cdn.a8.tvesou.com/right/recommend_video_list?leagueId=4&page=0&pageSize=20
    params[@"leagueId"] = @(type);
    params[@"page"] = @(0);
    params[@"pageSize"] = @(20);
    [HTTPManagers getWithLiveURL:@"recommend_video_list" params:params success:^(id json) {
        NSArray *arr= [json objectForKey:@"data"];
        if (self.videoArr.count>0) {
            [_videoArr removeAllObjects];
        }
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            JiJinModel *model = [JiJinModel mj_objectWithKeyValues:dict];
            [_videoArr addObject:model];
            
        }
        [_tableview reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
-(void)typeClick{
    JiJinTypeVC *vc = [JiJinTypeVC new];
    
    vc.selectBlock = ^(NSInteger i) {
        type = i;
        [self getTypeData];
    };

    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - tableview
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.isMoreVideo) {
        return self.moreVideoArr.count;
    }
    return self.videoArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.isMoreVideo) {
        NSDictionary *dict = self.moreVideoArr[indexPath.section];
        NSString *videoUrl = dict[@"videoUrl"];
        WEBViewController *web = [[WEBViewController alloc]init];
        web.titleStr = @"视屏";
        web.url = videoUrl;
        [self.navigationController pushViewController:web animated:YES];
    }else{
        NSInteger i = indexPath.section;
        JiJinModel *model = self.videoArr[indexPath.section];
        NSString *url = [[model.videos firstObject]objectForKey:@"url"];
        WEBViewController *web = [[WEBViewController alloc]init];
        web.titleStr = @"视屏";
        web.url = url;
        [self.navigationController pushViewController:web animated:YES];
        
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LiveListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LiveListTableViewCell class]) forIndexPath:indexPath];
    if (self.isMoreVideo) {
        NSDictionary *dict = self.moreVideoArr[indexPath.section];
        NSString *imagePath = [NSString stringWithFormat:@"http://freekicker-img.oss-cn-beijing.aliyuncs.com%@",dict[@"picUrl"]];
        [cell customImageUrlStr:imagePath title:dict[@"title"] time:dict[@"createTime"] commentatorStr:nil];
        
    }else{
        JiJinModel *model = self.videoArr[indexPath.section];
        NSString *imageUrlStr = [[model.videos firstObject]objectForKey:@"thumb"];
        [cell customImageUrlStr:imageUrlStr title:model.title time:model.source  commentatorStr:nil];
    }
    return cell;
}
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        [_tableview registerNib:[UINib nibWithNibName:NSStringFromClass([LiveListTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LiveListTableViewCell class])];
        _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if (self.isMoreVideo) {
                start=0;
                [self getVideoData];
            }else{
                [self getData];
            }
            
        }];
        _tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            if (self.isMoreVideo) {
                start++;
                [self getVideoData];
            }
            
            
        }];
    }
    return _tableview;
}
-(NSMutableArray *)videoArr{
    if (!_videoArr) {
        _videoArr = [NSMutableArray array];
    }
    return _videoArr;
}
-(NSMutableArray *)moreVideoArr{
    if (!_moreVideoArr) {
        _moreVideoArr = [NSMutableArray array];
    }
    return _moreVideoArr;
}

@end
